﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Common.Library;
using OnlineVideos.Library;
using OnlineVideos.UI;
using OnlineVideos.Common;
using OnlineVideos.Entities;
using OnlineVideos.Data;
using OnlineVideos.Utilities;
using Common.Utilities;
using System.Windows.Media.Imaging;
using System.Threading.Tasks;
namespace OnlineVideos.Views
{
    public partial class CharacterDetail : PhoneApplicationPage
    {

        #region Constructor
        public CharacterDetail()
        {
            try
            {
                InitializeComponent();
                LayoutRoot.Background = LoadCastPanoramaBackground(AppSettings.PersonID);
            }
            catch (Exception ex)
            {
                
                Exceptions.SaveOrSendExceptions("Exception in CharacterDetail Method In CharacterDetail.cs file.", ex);
            }
           
        }

        private Brush LoadCastPanoramaBackground(string p)
        {
          
            ImageBrush panoramabackground = new ImageBrush();
            string path = "";
            int personid = Convert.ToInt32(p);
            CastProfile Cast = new CastProfile();
            var topRatedList = Task.Run(async () => await Constants.connection.Table<CastProfile>().Where(i => i.PersonID == personid).ToListAsync()).Result;
            foreach (CastProfile itm in topRatedList)
            {
                path = itm.FlickrPanoramaImageUrl;
            }
            if (path != "")
            {
                BitmapImage PersonPanoramaImage = new BitmapImage();
                PersonPanoramaImage.UriSource = new Uri(path, UriKind.Absolute);
                panoramabackground.ImageSource = PersonPanoramaImage;
            }


            return panoramabackground;
        }
        #endregion

        #region PageLoad
        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {


            try
            {
                FlurryWP8SDK.Api.LogEvent("CharacterDetail Page", true);
                tblkVideosTitle.Text = OnlineShow.GetPersonDetail(AppSettings.PersonID).Name;
            }
            catch (Exception ex)
            {
                
               Exceptions.SaveOrSendExceptions("Exception in PhoneApplicationPage_Loaded Method In CharacterDetail.cs file.", ex);
            }
        }
        #endregion

        #region "Common Methods"

        protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
        {
            try
            {
                FlurryWP8SDK.Api.EndTimedEvent("CharacterDetail Page");
            }
            catch (Exception ex)
            {
                
               Exceptions.SaveOrSendExceptions("Exception in OnNavigatedFrom Method In CharacterDetail.cs file.", ex);
            }
        }
        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            try
            {
                FlurryWP8SDK.Api.LogPageView();
            }
            catch (Exception ex)
            {
                
                 Exceptions.SaveOrSendExceptions("Exception in OnNavigatedTo Method In CharacterDetail.cs file.", ex);
            }
        }
        protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
        {
            if (AppSettings.bcount)
            {
                e.Cancel = true;
                if (NavigationService.CanGoBack)
                    NavigationService.GoBack();
                //Persongal.popup.Close();
            }
            else
                AppSettings.detailtocast = 0;
        }
        #endregion

        #region Events
        private void imgTitle_MouseEnter(object sender, MouseButtonEventArgs e)
        {
            NavigationService.Navigate(NavigationHelper.MainPanoramaPage);
        }
        #endregion
    }
}